
export default class Http {
    static LOGIN_URL_PREFIX = "http://192.168.21.24:20000/"
    static HALL_URL_PREFIX = "http://192.168.21.24:20010/"

    get(url,data):Rx.Observable<any>{
        
        return Rx.Observable.fromPromise(new Promise<any>(function(resolve,reject){
            var xhr = new XMLHttpRequest();
            data = data || {}

            var formData = []
            for(var key in data){
                formData.push(key+"="+data[key])
            }
            
            xhr.open("GET", url+"?"+formData.join("&"), true);
            xhr.send();
           

            xhr.onreadystatechange = function () {
                
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    try{
                        
                        resolve(xhr.responseText)
                    }catch(err){
                        reject(err)
                    }
                   
                }
            };

            return new Promise<any>(function(){return xhr.onreadystatechange;});
            
            
        }));

        
        
    }

    getJson(url,data):Rx.Observable<any>{
        return Rx.Observable.fromPromise(new Promise<any>(function(resolve,reject){
            var xhr = new XMLHttpRequest();
            data = data || {}

            var formData = []
            for(var key in data){
                formData.push(key+"="+data[key])
            }
            
            xhr.open("GET", url+"?"+formData.join("&"), true);
            xhr.send();
           

            xhr.onreadystatechange = function () {
                
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    try{
                        
                        let res = JSON.parse(xhr.responseText)
                        if(res.success == 200){
                            resolve(res.data)
                        }else{
                            reject(res.message)
                        }
                    }catch(err){
                        reject(err)
                    }
                   
                }
            };

            return new Promise<any>(function(){return xhr.onreadystatechange;});
            
            
        }));
    }
    ajax(url,data):Rx.Observable<any>{
        
        return Rx.Observable.fromPromise(new Promise<any>(function(resolve,reject){
            var xhr = new XMLHttpRequest();
            data = data || {}

            var formData = new FormData();
            for(var key in data){
                formData.append(key,data[key])
            }
            
            xhr.open("POST", Http.LOGIN_URL_PREFIX+url, true);
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');  
            xhr.send(JSON.stringify(data));
           

            xhr.onreadystatechange = function () {
                
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    try{
                        resolve(xhr.responseText)
                    }catch(err){
                        reject(err)
                    }
                   
                }
            };

            return new Promise<any>(function(){return xhr.onreadystatechange;});
            
            
        }));

        
        
    }

}
